﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.Api.Write.GetCelim;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodsInfoAddCommandHandler : IRequestHandler<GoodsInfoAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodsInfo> _goodsbaseRepository;
        private readonly IIdContextCemil _idContextCemil;
        public GoodsInfoAddCommandHandler(IBaseRepository<GoodsInfo> goodsbaseRepository, IIdContextCemil idContextCemil)
        {
            _goodsbaseRepository = goodsbaseRepository;
            _idContextCemil = idContextCemil;
        }
        public async Task<APIResult<int>> Handle(GoodsInfoAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            GoodsInfo model=new GoodsInfo();
            model.Id=YitIdHelper.NextId();
            model.GoodsName=request.GoodsName;
            model.GoodsProductionbatches = request.GoodsProductionbatches;
            model.GoodsShelflife = request.GoodsShelflife;
            model.GoodsProductionLicense = request.GoodsProductionLicense;
            model.GoodsConpany = request.GoodsConpany;
            model.GoodsImg = request.GoodsImg;
            model.CreateName= _idContextCemil.UserName;
            var res= await _goodsbaseRepository.CreateAsync(model);
            result.Code=ApiEnum.Success;
            result.Msg = "商品信息添加成功";
            result.Data = res;
            return await Task.FromResult(result);
        }
    }
}
